/**
* @Title: medicalpersonnel.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEQI001
* @date 2014年8月19日 下午3:52:49
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('DoctorIntegrityCtrl',['$http','$scope','BaseURL','$state','$stateParams','BaseService','Modal',
	         	                           function($http,$scope,baseUrl,$state,$stateParams,BaseService,Modal){
		
		var URL = baseUrl;
		
		//form 查询条件对象
		$scope.form = {} ;
		$scope.form.doctorCodeStr = $stateParams.doctorCode;
		var ruleParentId = $stateParams.ruleParentId;
//根据父id找所有子id
		//		$http({
//	    	 method: "get", 
//	    	 cache:false,
//	    	 url: baseUrl +  'doctorIntegrity/getRuleId.do',
//	    	 params:ruleParentId
//		}).success(function(response, status, headers, config){
//			$scope.doctorCollection = response.list;
//			$scope.doctorTotal  = response.doctorTotal;
//			if($scope.doctorTotal <=0){
//				$scope.current = 0;
//			}
//			//清空单据数量
//			$scope.tableData = [];
//			$scope.totalCount = 0;
//			$scope.loading = false;
//		});
		//规则清单对象
		$scope.form.ruleArray = [];
		//初始化日期
		$scope.form.date={start:BaseService.getStartDate(),end:BaseService.getEndDate()};
		if($stateParams.startDate){
			$scope.form.date={start:$stateParams.startDate,end:$stateParams.endDate};
		}
		
		
		//表头
		$scope.columns = [
		  			    {label:'单据编号',map:'billNO',width:'14%',cellTemplate:'<a href="javascript:void(0);" ng:bind="data.billNO" ng-click="linkClick(data.billNO,data.visitNo);"></a>'},
		  			    {label:'就诊类型',map:'visitType',width:'10%'},
		  			    {label:'就诊科室',map:'medicalDeptName',width:'10%'},
		  			    {label:'医生',map:'doctorName',width:'10%'},
		  			    {label:'参保人',map:'name',width:'12%'},
		  			    {label:'年龄',map:'age',width:'6%'},
		  			    {label:'性别',map:'sex',width:'6%'},
		  			    {label:'入院日期',map:'inHospDate',width:'11%'},
		  			    {label:'出院日期',map:'outHospDate',width:'11%'},
		  			    {label:'单据金额',map:'sumAmount',width:'10%',cellTemplate:'<span ng-bind="data.sumAmountStr"></span>',cellClass:'text-right'}
		  			];
		
		//第几页
		$scope.currentPage = 1;
	
		$scope.form.ifShow = true;
		
		$scope.baseCollection = BaseService;
		BaseService.initHospitalsCollection();
		$scope.selectOption = {
				allowClear:true    //删除选中的的值
		};
		/**医疗机构级别*/
		BaseService.initMedicalOrgLevelCollection();
		
		/**医疗机构类别*/
		BaseService.initMedicalOrgTypeCollection();
		
		/**所在地区*/
		BaseService.initAreaCollection();
		
		/*** 就诊类型*/
		BaseService.initVisitTypeCollection();
		
		/** 险种*/
		BaseService.initInsuranceTypeCollection();
		
		/*** 性别*/
		BaseService.initSexCollection();
		
		/** 人员类型*/
		BaseService.initDoctorTypeCollection();
		//取得页面输入参数
		$scope.getParams = function(start,doctorCode,ruleId){
			
			var medicalOrgLevels = null;
			if($scope.form.medicalOrgLevel){
				medicalOrgLevels = $scope.getFieldCodes($scope.form.medicalOrgLevel);
			}
			
			var medicalOrgTypes = null;
			if($scope.form.medicalOrgType){
				medicalOrgTypes = $scope.getFieldCodes($scope.form.medicalOrgType);
			}
			var balanceStartDate = null;
			if($scope.form.date && angular.isUndefined($scope.form.date.start) == false){
				balanceStartDate = $scope.form.date.start;
			}
			var balanceEndDate = null;
			if($scope.form.date && angular.isUndefined($scope.form.date.end) == false){
				balanceEndDate = $scope.form.date.end;
			}
			var medicalOrgAreas = null;
			if($scope.form.areaCode){
				medicalOrgAreas = $scope.form.areaCode.code;
			}
			var visitType = null;
			if($scope.form.visitType){
				visitType = $scope.form.visitType.code;
			}
			/*var insuranceType = null;
			if($scope.form.insuranceType){
				insuranceType = $scope.form.insuranceType.code;
			}*/
			/*var doctorType = null;
			if($scope.form.doctorType){
				doctorType = $scope.form.doctorType.code;
			}*/
			
			/*var gender = null;
			
			if($scope.form.sex){
				gender = $scope.form.sex.code;
			}*/
			if(angular.isUndefined(ruleId) || ruleId == null || ruleId == ''){
				if($scope.form.ruleArray){
					ruleId = $scope.form.ruleArray.join(",");
				}
			}
			var doctorCodeList=[];
			if($scope.form.doctorCodeStr){
				doctorCodeList=$scope.form.doctorCodeStr.split(";");
			}
			var pcnoList=[];
			if($scope.form.pcnoStr){
				pcnoList=$scope.form.pcnoStr.split(";");
			}
			var params = {
					start:start,
					limit:20,
					medicalOrgLevels:medicalOrgLevels,
					medicalOrgTypes:medicalOrgTypes,
					medicalOrgAreas: medicalOrgAreas,
					medicalOrgCode:$scope.form.medicalOrgName,
					visitType:visitType,
					doctorName:$scope.form.doctorName,
					//insuranceType:insuranceType,
					name:$scope.form.name,
					doctorCodeList:doctorCodeList,
					pcnoList:pcnoList,
					//startAge:$scope.form.startAge,
					//endAge:$scope.form.endAge,
					//gender:gender,
					balanceStartDate:balanceStartDate,
					balanceEndDate:balanceEndDate,
					ifShow:$scope.form.ifShow,
					doctorCode:doctorCode,
					//doctorType:doctorType,
					ruleIds:ruleId
			};
			return  params;
		}
		//查询违规医生信息
		$scope.query = function (){
			var start = 0;
			if($scope.current > 1){
				start = ($scope.current - 1) * 20;
			}
			var params = $scope.getParams(start,null,null);
			if(params.doctorCodeList.length>30){
				Modal.alert('医生编号最多支持输入30个，用“;”隔开,请重新输入！');
				return;
			}
			if(params.pcnoList.length>30){
				Modal.alert('个人编号最多支持输入30个，用“;”隔开,请重新输入！');
				return;
			}
			var msg=BaseService.checkDoctorAndPcnoStr(params.doctorName,params.doctorCodeList,params.pcnoList);
			if(msg!=''&& msg.length>0){
				Modal.alert(msg);
				return;
			}
			var params = $scope.getParams(start,null,null);
			if(params.balanceEndDate==null || params.balanceEndDate==''){
				params.balanceEndDate=BaseService.getCurrentDate();
			}
			var checkTemp = BaseService.checkDateDiffYear(params.balanceStartDate,params.balanceEndDate);
			if(checkTemp!=''&& checkTemp.length>0){
				Modal.alert(checkTemp);
				return;
			}
			$scope.loading = true;
			$http({
		    	 method: "get", 
		    	 cache:false,
		    	 url: baseUrl +  'report/getDoctor.do',
		    	 params:params
			}).success(function(response, status, headers, config){
				$scope.doctorCollection = response.list;
				$scope.doctorTotal  = response.doctorTotal;
				if($scope.doctorTotal <=0){
					$scope.current = 0;
				}
				//清空单据数量
				$scope.tableData = [];
				$scope.totalCount = 0;
				$scope.loading = false;
			});
		}
		/**
		 * 页面查询
		 */
		$scope.onSubmit = function(){
			$scope.current = 1;
			$scope.query();
		}
		
		
		//查看单据明细
		$scope.linkClick = function(billNO,visitNo) {
			$state.go('billInfo.billInfoDetail',{
				billNO:billNO,
				visitNo:visitNo
    		});
		}
		//点击规则查询赋值
		$scope.getBillByDoctors = function(doctorCode,data){
			angular.forEach($scope.doctorCollection,function(doctor,index){
				angular.forEach(doctor.list,function(rule,index){
					rule.selected = false;
				});
			});
			data.selected = true;
			$scope.doctorCode = doctorCode;
			$scope.ruleId = data.ruleId;
			$scope.queryBillByParam();
		}
		
		$scope.$on('pageTool.skipPage',function(e,v){
			if(e.targetScope.id == 'doctorPage'){
				$scope.current = v;
				$scope.query();
			}else if(e.targetScope.id == 'billPage'){
				$scope.currentPage = v;
				$scope.queryBillByParam();
			}
		});
		
		//点击规则查询
		$scope.queryBillByParam = function (){
			var start = 0;
			if($scope.currentPage > 1){
				start = ($scope.currentPage - 1 )*20;
			}
			var ruleId = $scope.ruleId;
			var doctorCode = $scope.doctorCode;
			$scope.loading = true;
			$http({
				method:'get',
				cache:false,
				url:baseUrl+'report/getBillByDoctorOrRule.do',
				params:$scope.getParams(start,doctorCode,ruleId)
			})
			.success(function(response){
				$scope.tableData = response.root;
				$scope.totalCount = response.total;
				$scope.loading = false;
			});
		}
		//打开违反规则信息
		$scope.openRules = function (data){
			if(angular.isUndefined(data.list) || data.list == ''){
				$scope.loading = true;
				$http({
					method:'get',
					cache:false,
					url:baseUrl + 'report/getRuleByDoctor.do',
					params:$scope.getParams(null,data.doctorCode,null)
				})
				.success(function(response){
					data.list = response;
					$scope.loading = false;
				});
			}
			data.show = !data.show;
		}
		
		//重置按钮事件
		$scope.resetBut = function(){
			$scope.form = {};
			$scope.form.date={start:BaseService.getStartDate(),end:BaseService.getEndDate()};
			$scope.form.ifShow = true;
			$scope.form.ruleArray = [];
		}
		

		//获取代码表code集合
		$scope.getFieldCodes = function(datas){
			var codes = [];
			angular.forEach(datas,function(data,i){
				codes.push(data.code);
			});
			return codes.join(",");
		}
		
		
	}]);
})();